/**
 *    TimedProfiles - let Android perform tasks based on conditions
 *    Copyright (C) 2011  Daniel Weinhold
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * */
package de.drak.Profiles;

import java.util.Map;


import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**ExpandableListAdapter for Rule and Profile Editor*/
public class EditorListAdapter extends BaseExpandableListAdapter {
	private LayoutInflater mInflater;
	
	private Map<Long, CharSequence> db_name = null;
	private Map<Long, CharSequence> db_text = null;
	private long[][] db_id = null;
	private Map<Long, Drawable> db_icon = null;
	private static class ViewHolder {
		 TextView text;
		 TextView text2;
		 ImageView icon;
	 }
	 public EditorListAdapter(Context context, String[] group, long[][] id, Map<Long, CharSequence> name, Map<Long, CharSequence> text, Map<Long, Drawable> icon) {
		 mInflater = LayoutInflater.from(context);

		 
			//new button
			db_id = id;
			db_text = text;
			db_name = name;
			db_icon = icon;
			groups = group;
			
			
	 }

    private String[] groups = null;
    
    public Object getChild(int groupPosition, int childPosition) {
        return db_name.get(getChildId(groupPosition,childPosition));
    }

    public long getChildId(int groupPosition, int childPosition) {
        return db_id[groupPosition][childPosition];
    }

    public int getChildrenCount(int groupPosition) {
        return db_id[groupPosition].length;
    }

    public Object getGroup(int groupPosition) {
        return groups[groupPosition];
    }

    public int getGroupCount() {
        return groups.length;
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public boolean hasStableIds() {
        return true;
    }

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
			ViewGroup parent) {

		 ViewHolder holder;
		 if (convertView == null) {
			 convertView = mInflater.inflate(R.layout.row, null);
			 holder = new ViewHolder();
			 
			 holder.text = (TextView) convertView.findViewById(R.id.firstLine);
			 holder.icon = (ImageView) convertView.findViewById(R.id.icon);
			 holder.text2 = (TextView) convertView.findViewById(R.id.secondLine);
			 
			 convertView.setTag(holder);
		 } else {
			 holder = (ViewHolder) convertView.getTag();
		 }

		 long id = getChildId(groupPosition, childPosition);
		 
		 holder.icon.setImageDrawable(db_icon.get(id));
		 holder.text.setText(db_name.get(id));
		 
		 if(db_text!=null){
			 if(db_text.get(id) == null || db_text.get(id).length() < 1)
			 {
				 holder.text2.setVisibility(View.GONE);
			 }else{
				 holder.text2.setVisibility(View.VISIBLE);
				 holder.text2.setText(db_text.get(id));				 
			 } 
		 }else{
			 holder.text2.setVisibility(View.GONE);
		 }
	
		 return convertView;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        // Layout parameters for the ExpandableListView
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, 32);

        TextView textView = new TextView(parent.getContext());
        textView.setLayoutParams(lp);
        // Center the text vertically
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        // Set the text starting position
        
        textView.setText(getGroup(groupPosition).toString());
        textView.setTextColor(Color.WHITE);
        textView.setBackgroundResource(android.R.drawable.title_bar);
        textView.setPadding(36, 0, 0, 0);
        return textView;
	}
}
